package com.yunhai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.model.entity.sys.Feedback;
import com.yunhai.model.entity.sys.SysVersionLastEntity;
import com.yunhai.model.entity.sys.SysVersionManageEntity;
import com.yunhai.system.mapper.SysVersionLastMapper;
import com.yunhai.system.mapper.VersionManageMapper;
import com.yunhai.system.service.VersionManageService;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.model.entity.sys.VersionInfo;
import com.yunhai.model.vo.sys.SysVersionManageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class VersionManageServiceImpl implements VersionManageService {

    @Resource
    private VersionManageMapper versionManageMapper;
    @Resource
    private SysVersionPartServiceImpl sysVersionPartService;
    @Resource
    private SysVersionLastMapper sysVersionLastMapper;

    public void addVersionInfo(SysVersionManageVo sysVersionManageVo) throws Exception {
        VersionInfo versionInfo = new VersionInfo();
        ReflectionCopy.copyProperties( versionInfo, sysVersionManageVo );
        versionManageMapper.insert(versionInfo);
    }

    @Override
    @Transactional
    public void addVersionInfo(MultipartFile file, SysVersionManageVo sysVersionManageVo) throws Exception {

        //处理文件
        sysVersionPartService.saveVersionContentWithFile(file, sysVersionManageVo);
        //存储总的版本
        VersionInfo versionInfo = new VersionInfo();
        ReflectionCopy.copyProperties( versionInfo, sysVersionManageVo );

        versionManageMapper.insert(versionInfo);
    }

    @Override
    public VersionInfo getVersionDetail(Long id) throws Exception {

        VersionInfo versionInfo = versionManageMapper.selectById(id);
        return versionInfo;
    }


    public IPage getVersionPagination(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<VersionInfo> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(VersionInfo::getCreateTime);
        versionManageMapper.selectPage(page, lqs);

        return page;
    }

    @Override
    public void batchRemoveVersionInfo(List<Long> ids) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        versionManageMapper.deleteBatchIds(ids);

    }
}
